#!/usr/bin/env bash

set -eEo pipefail

SCRIPTPATH="$(
  cd "$(dirname "$0")"
  pwd -P
)"

# shellcheck source=ci/docker_buildx_commands
source "${SCRIPTPATH}/docker_buildx_commands"
# shellcheck source=ci/.colors
source "${SCRIPTPATH}/.colors"

TARGET_FLAVOR=$1
TARGET_ARCH=$2
TARGET_FILE=$3

if [ -z "${TARGET_ARCH}" ] || [ -z "${TARGET_FILE}" ]; then
    echo -e "${RED}Missing required arguments. Usage: build_helper_docker TARGET_ARCH TARGET_FILE${RST}"
    exit 1
fi

DOCKERFILE="Dockerfile.${TARGET_FLAVOR}"

if [[ $IMAGE_SHELL == 'pwsh' ]]; then
    if [ -z "${TARGET_FLAVOR}" ] || [ -z "${PWSH_VERSION}" ] || [ -z "${PWSH_TARGET_FLAVOR_IMAGE_VERSION}" ] || [ -z "${PWSH_IMAGE_DATE}" ]; then
        echo -e "${RED}Missing required arguments. When IMAGE_SHELL is 'pwsh', PWSH_VERSION, " \
        "TARGET_FLAVOR, PWSH_TARGET_FLAVOR_IMAGE_VERSION, and PWSH_IMAGE_DATE must be defined${RST}"
         exit 1
    fi

    # NOTE: To find the most recent Powershell Core tag that supports the desired Powershell Core version on the
    # desired ${TARGET_FLAVOR} version, run the following command:
    # ```
    # export PWSH_VERSION="7.1.1"
    # export PWSH_TARGET_FLAVOR_IMAGE_VERSION="3.12"
    # curl -sL https://mcr.microsoft.com/v2/powershell/tags/list | \
    #   jq -r '.tags[]' | \
    #   grep "${PWSH_VERSION}-${TARGET_FLAVOR}-${PWSH_TARGET_FLAVOR_IMAGE_VERSION}" | \
    #   tail -n 1
    # ```
    BASE_IMAGE="mcr.microsoft.com/powershell:${PWSH_VERSION}-${TARGET_FLAVOR}-${PWSH_TARGET_FLAVOR_IMAGE_VERSION}-${PWSH_IMAGE_DATE}"
else
    if [ -z "${TARGET_FLAVOR}" ] || [ -z "${TARGET_FLAVOR_IMAGE_VERSION}" ]; then
        echo -e "${RED}Missing required arguments. TARGET_FLAVOR and TARGET_FLAVOR_IMAGE_VERSION must be defined${RST}"
        exit 1
    fi

    BASE_IMAGE="${TARGET_FLAVOR}:${TARGET_FLAVOR_IMAGE_VERSION}"
fi

REVISION=${REVISION:-}
if [[ -z "${REVISION}" ]]; then
    REVISION=$(git rev-parse --short=8 HEAD || echo "unknown")
fi

case "${TARGET_ARCH}" in
    "x86_64")
        platform_arch='amd64'
        ;;
    *)
        platform_arch="${TARGET_ARCH}"
        ;;
esac

binary_file="out/binaries/gitlab-runner-helper/gitlab-runner-helper.${TARGET_ARCH}"

if [ ! -f "$binary_file" ]; then
    echo -e "${RED}Missing binary file. You probably need to run 'make helper-bin'.${RST}"
    exit 1
fi

cp "$binary_file" dockerfiles/runner-helper/binaries/gitlab-runner-helper
chmod +x dockerfiles/runner-helper/binaries/gitlab-runner-helper

os=$(_docker version -f '{{.Server.Os}}')
platform="${os}/${platform_arch}"

echo -e "Building helper image for: ${GRN}${platform}${RST} based on ${GRN}${BASE_IMAGE}${RST}"

trap cleanup_docker_context_trap ERR SIGINT SIGTERM
setup_docker_context

_docker_buildx build \
    --platform "${platform}" \
    --no-cache \
    --build-arg "BASE_IMAGE=${BASE_IMAGE}" \
    --output "type=tar,dest=$TARGET_FILE" \
    --tag "gitlab/gitlab-runner-helper:$TARGET_ARCH-$REVISION" \
    --build-arg http_proxy="${http_proxy}" \
    --file "dockerfiles/runner-helper/${DOCKERFILE}" \
    dockerfiles/runner-helper

trap - ERR SIGINT SIGTERM
cleanup_docker_context
